#ifdef KITSCHY_DEBUG_MEMORY 
#include "debug_memorymanager.h"
#endif

#ifdef WIN32
#include "windows.h"
#endif

#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "math.h"

#include <GL/gl.h>
#include <GL/glut.h>

#include "BList.h"
#include "BVector.h"
#include "geometrics.h"
#include "quaternion.h"
#include "symbol.h"
#include "sort.h"
#include "ontology.h"
#include "featureterm.h"
#include "FTKBase.h"

#include "myglutaux.h"
#include "FTGLPolygonFont.h"

#include "AWEntity.h"
#include "AWMessage.h"
#include "AWScenario.h"
#include "AWSelectScenario.h"
#include "AWSelectMPlaneScenario.h"

#include "loader.h"

AW_SelectMPlaneScenario::AW_SelectMPlaneScenario() : AW_SelectScenario() 
{
	class_name->set("AW_SelectMPlaneScenario");

	nplanes=1;
	pname=new Symbol *[1];
	startx=new float[1];
	startz=new float[1];
	sizex=new float[1];
	sizez=new float[1];
	r=new float[1];
	g=new float[1];
	b=new float[1];
	startx[0]=-1;
	startz[0]=-1;
	sizex[0]=2;
	sizez[0]=2;
	r[0]=0.5F;
	g[0]=0.5F;
	b[0]=0.5F;
	nbridges=0;
	bname=0;
	bplanea=bplaneb=0;
	bstartx=bstartz=bsizex=bsizez=br=bg=bb=bpax=bpaz=bpbx=bpbz=0;

	font=0;

} /* AW_SelectMPlaneScenario::AW_SelectMPlaneScenario */ 


AW_SelectMPlaneScenario::AW_SelectMPlaneScenario(FILE *fp) : AW_SelectScenario() 
{
	class_name->set("AW_SelectMPlaneScenario");

	font=0;

	load(fp);
} /* AW_SelectMPlaneScenario::AW_SelectMPlaneScenario */ 


AW_SelectMPlaneScenario::~AW_SelectMPlaneScenario()
{
	if (nplanes!=0) {
		int i;
		for(i=0;i<nplanes;i++) {
			delete pname[i];
			pname[i]=0;
		} /* for */ 
		delete []pname;
		delete []startx;
		delete []startz;
		delete []sizex;
		delete []sizez;
		delete []r;
		delete []g;
		delete []b;
	} /* if */ 
	if (nbridges!=0) {
		delete []bname;
		delete []bplanea;
		delete []bplaneb;
		delete []bstartx;
		delete []bstartz;
		delete []bsizex;
		delete []bsizez;
		delete []br;
		delete []bg;
		delete []bb;
		delete []bpax;
		delete []bpaz;
		delete []bpbx;
		delete []bpbz;
	} /* if */ 

	if (font!=0) {
//		delete font;
		font=0;
	} /* if */ 
} /* AW_SelectMPlaneScenario::~AW_SelectMPlaneScenario */ 


bool AW_SelectMPlaneScenario::load(FILE *fp) 
{
	int i;

	if (!AW_SelectScenario::load(fp)) return false;
	if (1!=fscanf(fp,"%i",&nplanes)) return false;

	pname=new Symbol *[nplanes];
	startx=new float[nplanes];
	startz=new float[nplanes];
	sizex=new float[nplanes];
	sizez=new float[nplanes];
	r=new float[nplanes];
	g=new float[nplanes];
	b=new float[nplanes];
	for(i=0;i<nplanes;i++) {
		pname[i]=new Symbol(fp); 
		if (2!=fscanf(fp,"%f %f",&startx[i],&startz[i])) return false;
		if (2!=fscanf(fp,"%f %f",&sizex[i],&sizez[i])) return false;
		if (3!=fscanf(fp,"%f %f %f",&r[i],&g[i],&b[i])) return false;
	} /* for */ 

	if (1!=fscanf(fp,"%i",&nbridges)) return false;

	if (nbridges>0) {
		bname=new Symbol *[nbridges]; 
		bplanea=new int[nbridges]; 
		bplaneb=new int[nbridges]; 
		bstartx=new float[nbridges]; 
		bstartz=new float[nbridges]; 
		bsizex=new float[nbridges];
		bsizez=new float[nbridges];
		br=new float[nbridges];
		bg=new float[nbridges];
		bb=new float[nbridges];
		bpax=new float[nbridges];
		bpaz=new float[nbridges];
		bpbx=new float[nbridges];
		bpbz=new float[nbridges];
		for(i=0;i<nbridges;i++) {
			bname[i]=new Symbol(fp);
			if (2!=fscanf(fp,"%i %i",&bplanea[i],&bplaneb[i])) return false;
			if (2!=fscanf(fp,"%f %f",&bstartx[i],&bstartz[i])) return false;
			if (2!=fscanf(fp,"%f %f",&bsizex[i],&bsizez[i])) return false;
			if (3!=fscanf(fp,"%f %f %f",&br[i],&bg[i],&bb[i])) return false;
			if (4!=fscanf(fp,"%f %f %f %f",&bpax[i],&bpaz[i],&bpbx[i],&bpbz[i])) return false;
		} /* for */ 
	} /* if */ 

	return true;
} /* AW_SelectMPlaneScenario::load */ 


bool AW_SelectMPlaneScenario::save(FILE *fp) 
{
	int i;

	if (!AW_SelectScenario::save(fp)) return false;
	fprintf(fp,"%i\n",nplanes);
	for(i=0;i<nplanes;i++) {
		if (!pname[i]->save(fp)) return false;
		fprintf(fp,"%f %f\n",startx[i],startz[i]);
		fprintf(fp,"%f %f\n",sizex[i],sizez[i]);
		fprintf(fp,"%f %f %f\n",r[i],g[i],b[i]);
	} /* for */ 
	fprintf(fp,"%i\n",nbridges);
	for(i=0;i<nbridges;i++) {
		if (!bname[i]->save(fp)) return false;
		fprintf(fp,"%i %i\n",bplanea[i],bplaneb[i]);
		fprintf(fp,"%f %f\n",bstartx[i],bstartz[i]);
		fprintf(fp,"%f %f\n",bsizex[i],bsizez[i]);
		fprintf(fp,"%f %f %f\n",br[i],bg[i],bb[i]);
		fprintf(fp,"%f %f %f %f\n",bpax[i],bpaz[i],bpbx[i],bpbz[i]);
	} /* for */ 


	return true;
} /* AW_SelectMPlaneScenario::save */ 


bool AW_SelectMPlaneScenario::is_a(char *c)
{
	if (strcmp(c,"AW_SelectMPlaneScenario")==0) return true;

	return AW_SelectScenario::is_a(c);
} /* AW_SelectMPlaneScenario::is_a */ 


bool AW_SelectMPlaneScenario::is_a(Symbol *c)
{
	if (c->cmp("AW_SelectMPlaneScenario")) return true;

	return AW_SelectScenario::is_a(c);
} /* AW_SelectMPlaneScenario::is_a */ 


int AW_SelectMPlaneScenario::selection_get_num(void)
{
	return nplanes;
} /* AW_SelectMPlaneScenario::selection_get_num */ 


bool AW_SelectMPlaneScenario::set_selected(int name,float *cx,float *cy,float *cz,
												    float *cxp,float *cyp,float *czp)
{
	int selected=name-m_firstname;

	if (selected>=0 && selected<nplanes) {
		/* Set the proper camera position: */ 
		/* base: */ 
		*cx=startx[selected]+sizex[selected];
		*cy=-1.0F*(sizex[selected]+sizez[selected]);
		*cz=startz[selected]-2.5F*sizez[selected];

		/* center: */ 
		*cxp=startx[selected];
		*cyp=0.0;
		*czp=startz[selected];

		return true;
	} /* if */ 

	return false;
} /* AW_SelectMPlaneScenario::set_selected */ 


void AW_SelectMPlaneScenario::draw(void)
{
	int i;

	if (font==0) {
		FILE *fp=fopen("arial.ttf","r");
		if (fp!=0) {
			fclose(fp);
			font=new FTGLPolygonFont("arial.ttf");
			font->FaceSize(2);
			font->Depth(0.2F);
		} /* if */ 
	} /* if */ 

	/* Escenario: */ 
	glNormal3f(0.0,-1.0,0.0);

	for(i=0;i<nplanes;i++) {
		glPushName(m_firstname+i);
		glColor3f(r[i],g[i],b[i]);
		glMatrixMode(GL_MODELVIEW);
		glPushMatrix();
		glTranslatef(startx[i],0,startz[i]);

		/* Dibujar plano: */ 
		glPushMatrix();
		glTranslatef(0,0.2F,0);
		glutSolidBox(sizex[i],0.2F,sizez[i]);
		glPopMatrix();

		/* Dibujar nombre del plano: */ 
		glPushMatrix();
		glTranslatef(0,0,-(sizez[i]+0.5F));

		if (font!=0) {
			float llx,lly,llz,urx,ury,urz;

			glColor3f(1,1,1);
			glPushMatrix();
			font->BBox( pname[i]->get(), llx, lly, llz, urx, ury, urz);		
			glTranslatef(-(urx-llx)/2,-0.2F,1.2F);
			glRotatef(90,1,0,0);
			font->Render(pname[i]->get());
			glPopMatrix();
		} /* if */ 

		glPopMatrix();

		glPopMatrix();
		glPopName();
	} /* for */ 


	for(i=0;i<nbridges;i++) {
		glColor3f(br[i],bg[i],bb[i]);
		glMatrixMode(GL_MODELVIEW);
		glPushMatrix();
		glTranslatef(bstartx[i],0.5F,bstartz[i]);
		glBegin(GL_TRIANGLES);
		glVertex3f(-bsizex[i],0,-bsizez[i]);
		glVertex3f(bsizex[i],0,-bsizez[i]);
		glVertex3f(bsizex[i],0,bsizez[i]);

		glVertex3f(bsizex[i],0,bsizez[i]);
		glVertex3f(-bsizex[i],0,bsizez[i]);
		glVertex3f(-bsizex[i],0,-bsizez[i]);
		glEnd();
		glPopMatrix();
	} /* for */ 
} /* AW_SelectMPlaneScenario::draw */ 

